package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pojo.User;

import dao.UserDAO;

import java.text.ParseException;
import java.util.*;

import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
@Controller
public class DangKiController {

    @RequestMapping(value= "/DangKi.do" , method = RequestMethod.GET)
    public String getview(HttpServletRequest request
    		) {
    	ReCaptcha c = ReCaptchaFactory.newReCaptcha(
				"6Ldjn9oSAAAAAK2FKcLUewjwChpB_3xmCcnByDYp",
				"6Ldjn9oSAAAAAGsu7CZ0AZVFvIsnhSFqyaM6uEU2", false);
		request.setAttribute("captchar", c);

		return "page/FrontEnd/DangKi";
    	
    }
   

	@RequestMapping(value = "DangKi.do", method = RequestMethod.POST)
	public @ResponseBody String show(
			@RequestParam(value = "HoVaTen", required = false) String hoTen,
			@RequestParam(value = "Ngay", required = false) String ngay,
			@RequestParam(value = "Thang", required = false) String thang,
			@RequestParam(value = "Nam", required = false) String nam,
			@RequestParam(value = "DiaChi", required = false) String diaChi,
			@RequestParam(value = "Email", required = false) String email,
			@RequestParam(value = "DienThoai", required = false) String dienThoai,
			@RequestParam(value = "TenDangNhap", required = false) String tenDangNhap,
			@RequestParam(value = "MatKhau", required = false) String matKhau,
			HttpServletRequest request) {
		Boolean insertSuccess = true;
		UserDAO khDAO = new UserDAO();

		HttpSession session = request.getSession();

		insertSuccess = khDAO.insertUser(hoTen, ngay, thang, nam,
				diaChi, email, dienThoai, tenDangNhap, matKhau);
		if (insertSuccess) {
			synchronized (session) {
				User khachHang = (User) session
						.getAttribute("user");
				if (khachHang == null) {
					khachHang = khDAO.getUser(tenDangNhap);
					session.setAttribute("user", khachHang);
				}
			}
			return "success";
		} else {
			return "fail";
		}
	}

	@RequestMapping(value = "KiemTraCaptcha.do", method = RequestMethod.POST)
	public @ResponseBody
	String hienThi(HttpServletRequest request) {
		String remoteAddr = request.getRemoteAddr();
		ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
		reCaptcha.setPrivateKey("6Ldjn9oSAAAAAGsu7CZ0AZVFvIsnhSFqyaM6uEU2");

		String challenge = request.getParameter("recaptcha_challenge_field");
		String uresponse = request.getParameter("recaptcha_response_field");
		ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr,
				challenge, uresponse);

		if (reCaptchaResponse.isValid()) {
			return ("correct");
		} else {
			return ("wrong");
		}
	}
   
}